.TH std::array 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::array \- std::array

.SH Synopsis
   Defined in header <array>
   template<

       class T,               \fI(since C++11)\fP
       std::size_t N

   > struct array;

   std::array is a container that encapsulates fixed size arrays.

   This container is an aggregate type with the same semantics as a struct holding a
   C-style array T[N] as its only non-static data member. Unlike a C-style array, it
   doesn't decay to T* automatically. As an aggregate type, it can be initialized with
   aggregate-initialization given at most N initializers that are convertible to T:
   std::array<int, 3> a = {1, 2, 3};.

   The struct combines the performance and accessibility of a C-style array with the
   benefits of a standard container, such as knowing its own size, supporting
   assignment, random access iterators, etc.

   std::array satisfies the requirements of Container and ReversibleContainer except
   that default-constructed array is not empty and that the complexity of swapping is
   linear,
   satisfies the requirements of ContiguousContainer,
   \fI(since C++17)\fP and partially satisfies the requirements of SequenceContainer.

   There is a special case for a zero-length array (N == 0). In that case,
   array.begin() == array.end(), which is some unique value. The effect of calling
   front() or back() on a zero-sized array is undefined.

   An array can also be used as a tuple of N elements of the same type.

.SH Template parameters

   T - element type Must be MoveConstructible and MoveAssignable.
   N - the number of elements in the array or 0.

    This section is incomplete
    Reason: Complete the descriptions of template parameters.

.SH Member types

   Member type            Definition
   value_type             T
   size_type              std::size_t
   difference_type        std::ptrdiff_t
   reference              value_type&
   const_reference        const value_type&
   pointer                value_type*
   const_pointer          const value_type*
                          LegacyRandomAccessIterator and                  \fI(until C++17)\fP
                          LegacyContiguousIterator to value_type
                          LegacyRandomAccessIterator and                  \fI(since C++17)\fP
                          LegacyContiguousIterator that is a LiteralType  \fI(until C++20)\fP
   iterator               to value_type
                          LegacyRandomAccessIterator,
                          contiguous_iterator, and ConstexprIterator to   \fI(since C++20)\fP
                          value_type


                          LegacyRandomAccessIterator and                  \fI(until C++17)\fP
                          LegacyContiguousIterator to const value_type
                          LegacyRandomAccessIterator and                  \fI(since C++17)\fP
                          LegacyContiguousIterator that is a LiteralType  \fI(until C++20)\fP
   const_iterator         to const value_type
                          LegacyRandomAccessIterator,
                          contiguous_iterator, and ConstexprIterator to   \fI(since C++20)\fP
                          const value_type


   reverse_iterator       std::reverse_iterator<iterator>
   const_reverse_iterator std::reverse_iterator<const_iterator>

.SH Member functions

.SH Implicitly-defined member functions
                         initializes the array following the rules of aggregate
   constructor           initialization (note that default initialization may result in
   (implicitly declared) indeterminate values for non-class T)
                         \fI(public member function)\fP
   destructor            destroys every element of the array
   (implicitly declared) \fI(public member function)\fP
   operator=             overwrites every element of the array with the corresponding
   (implicitly declared) element of another array
                         \fI(public member function)\fP
.SH Element access
   at                    access specified element with bounds checking
                         \fI(public member function)\fP
   operator[]            access specified element
                         \fI(public member function)\fP
   front                 access the first element
                         \fI(public member function)\fP
   back                  access the last element
                         \fI(public member function)\fP
   data                  direct access to the underlying contiguous storage
                         \fI(public member function)\fP
.SH Iterators
   begin                 returns an iterator to the beginning
   cbegin                \fI(public member function)\fP
   end                   returns an iterator to the end
   cend                  \fI(public member function)\fP
   rbegin                returns a reverse iterator to the beginning
   crbegin               \fI(public member function)\fP
   rend                  returns a reverse iterator to the end
   crend                 \fI(public member function)\fP
.SH Capacity
   empty                 checks whether the container is empty
                         \fI(public member function)\fP
   size                  returns the number of elements
                         \fI(public member function)\fP
   max_size              returns the maximum possible number of elements
                         \fI(public member function)\fP
.SH Operations
   fill                  fill the container with specified value
                         \fI(public member function)\fP
   swap                  swaps the contents
                         \fI(public member function)\fP

.SH Non-member functions

   operator==
   operator!=
   operator<
   operator<=
   operator>
   operator>=
   operator<=>               lexicographically compares the values of two arrays
   \fI(C++11)\fP                   \fI(function template)\fP
   \fI(C++11)\fP(removed in C++20)
   \fI(C++11)\fP(removed in C++20)
   \fI(C++11)\fP(removed in C++20)
   \fI(C++11)\fP(removed in C++20)
   \fI(C++11)\fP(removed in C++20)
   (C++20)
   get(std::array)           accesses an element of an array
   \fI(C++11)\fP                   \fI(function template)\fP
   std::swap(std::array)     specializes the std::swap algorithm
   \fI(C++11)\fP                   \fI(function template)\fP
   to_array                  creates a std::array object from a built-in array
   (C++20)                   \fI(function template)\fP

.SH Helper classes

   std::tuple_size<std::array>    obtains the size of an array
   \fI(C++11)\fP                        \fI(class template specialization)\fP
   std::tuple_element<std::array> obtains the type of the elements of array
   \fI(C++11)\fP                        \fI(class template specialization)\fP

     Deduction guides \fI(since C++17)\fP

.SH Example


// Run this code

 #include <algorithm>
 #include <array>
 #include <iostream>
 #include <iterator>
 #include <string>

 int main()
 {
     // Construction uses aggregate initialization
     std::array<int, 3> a1{{1, 2, 3}}; // Double-braces required in C++11 prior to
                                       // the CWG 1270 revision (not needed in C++11
                                       // after the revision and in C++14 and beyond)

     std::array<int, 3> a2 = {1, 2, 3}; // Double braces never required after =

     // Container operations are supported
     std::sort(a1.begin(), a1.end());
     std::ranges::reverse_copy(a2, std::ostream_iterator<int>(std::cout, " "));
     std::cout << '\\n';

     // Ranged for loop is supported
     std::array<std::string, 2> a3{"E", "\\u018E"};
     for (const auto& s : a3)
         std::cout << s << ' ';
     std::cout << '\\n';

     // Deduction guide for array creation \fI(since C++17)\fP
     [[maybe_unused]] std::array a4{3.0, 1.0, 4.0}; // std::array<double, 3>

     // Behavior of unspecified elements is the same as with built-in arrays
     [[maybe_unused]] std::array<int, 2> a5; // No list init, a5[0] and a5[1]
                                             // are default initialized
     [[maybe_unused]] std::array<int, 2> a6{}; // List init, both elements are value
                                               // initialized, a6[0] = a6[1] = 0
     [[maybe_unused]] std::array<int, 2> a7{1}; // List init, unspecified element is value
                                                // initialized, a7[0] = 1, a7[1] = 0
 }

.SH Output:

 3 2 1
 E Ǝ

.SH See also

   make_array                   creates a std::array object whose size and optionally
   (library fundamentals TS v2) element type are deduced from the arguments
                                \fI(function template)\fP

.SH Category:
     * Todo with reason
